/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data.Frontier;

import CrlException.CrlException;
import java.util.*;

/**
 *
 * @author CUHUUHOANG
 */
public class URLData {

    private static int URLProcessed;
    static Queue<String> queueURL;
    static Hashtable<String, Integer> urlDepth;
    public static boolean isUsing;

    public URLData() throws CrlException {
        try {
            urlDepth = new Hashtable<String, Integer>();
            queueURL = new LinkedList<String>();
            isUsing = false;
            URLProcessed = 0;


        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public void addURL(String link, int depth) throws CrlException {
        try {
            if (isUsing) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new CrlException(e.getMessage());
                }
            }
            isUsing = true;
            //do job
            if (!queueURL.contains(link)) {
                URLData.queueURL.add(link);
                URLData.urlDepth.put(link, depth);
            }
            //end job
            isUsing = false;
            notifyAll();
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public URLObject getURL() throws CrlException {
        try {
            if (isUsing) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new CrlException(e.getMessage());
                }
            }
            isUsing = true;
            //do job
            String link = URLData.queueURL.poll();
            //URLProcessed++;
            int depth = 0;
            if (link != null) {
                depth = URLData.urlDepth.get(link);
                if (URLData.urlDepth.contains(link)) {
                    URLData.urlDepth.remove(link);
                }
            }
            //end job
            isUsing = false;
            notifyAll();
            if (link == null) {
                return null;
            }
            return new URLObject(link, depth);
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    public void addURL(URLObject urlO) throws CrlException {
        try {
            String link = urlO.link;
            int depth = urlO.depth;
            addURL(link, depth);
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public int getSize() throws CrlException {
        try {
            if (isUsing) wait();
            isUsing = true;
            //do job
            int out = URLData.queueURL.size();
            //end job
            isUsing = false;
            notifyAll();
            return out;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public boolean isContainURL(String link) throws CrlException {
        try {
            if (isUsing) wait();
            isUsing = true;
            //do job
            boolean out = queueURL.contains(link);
            //end job
            isUsing = false;
            notifyAll();
            return out;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public int getURLProcessed() throws CrlException {

        try {
            if (isUsing) wait();
            isUsing = true;
            //do job
            int out = URLProcessed;
            //end job
            isUsing = false;
            notifyAll();
            return out;
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }

    synchronized public void increaseURLProcessed() throws CrlException {

        try {
            if (isUsing) wait();
            isUsing = true;
            //do job
            URLProcessed++;
            //end job
            isUsing = false;
            notifyAll();
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }
}
